# /bin/bash


HOME_DIR=$(cd $(dirname $0);cd ../;pwd)
echo $HOME_DIR
IMAGES_DIR=${HOME_DIR}/images

DEPS_PATRONI=${HOME_DIR}/deps/patroni
echo "$DEPS_PATRONI" 


function usage() {
  cat << EOF

Usage: build.sh -v [version]  [-i]  [Docker build option]
Builds a Docker Image for Postgres with patroni
  
Parameters:
   -v: version to build

LICENSE MulanPSL2
EOF
}

function build_image(){
  IMAGE_NAME=$1
  dockerfile=$2
  VERSION=$3
  pwd
  echo "Building image '$IMAGE_NAME' with dockerfile ${dockerfile} ..."

  # BUILD THE IMAGE (replace all environment variables)
  BUILD_START=$(date '+%s')
  docker build --force-rm=true --no-cache=true \
       $DOCKEROPS \
       --build-arg="GAUSS_VERSION=${VERSION}"  \
       -t $IMAGE_NAME -f $dockerfile . || {
  echo ""
  echo "ERROR: partroni postgres Database Docker Image was NOT successfully created."
  echo "ERROR: Check the output and correct any reported problems with the docker build operation."
  exit 1
  }
}

if [ -d "${DEPS_PATRONI}" ];then
    echo "Update patroni in git submodule"
    cd ${DEPS_PATRONI} 
    git pull
    cd -
else
    echo "Init patroni in git submodule"
    git submodule update --init --recursive
fi


function build_image(){
  IMAGE_NAME=$1
  dockerfile=$2
  SKIP_LOCAL_MIRROR=$3
  VERSION=$4
  pwd
  echo "Building image '$IMAGE_NAME' with dockerfile ${dockerfile} ..."

  # BUILD THE IMAGE (replace all environment variables)
  BUILD_START=$(date '+%s')
  docker build --force-rm=true --no-cache=true \
       $DOCKEROPS \
       --build-arg="SKIP_LOCAL_MIRROR=${SKIP_LOCAL_MIRROR}"  \
       -t $IMAGE_NAME -f $dockerfile . || {
  echo ""
  echo "ERROR: openGauss Database Docker Image was NOT successfully created."
  echo "ERROR: Check the output and correct any reported problems with the docker build operation."
  exit 1
  }
}



##############
#### MAIN ####
##############

# docker build  -t gauss-openeuler-os:22.03 -f Dockerfile_os .



function main() {
  arch=""
  case $(uname -m) in 
    i386 ) arch="386";;
    i686 ) arch="386" ;; 
    x86_64 ) arch="amd64";; 
    aarch64)  arch="arm64";;  
    arm64  )  arch="arm64"  
  esac
  
 
  if [ "${arch}" = "amd64" ]; then
    DOCKERFILE="Dockerfile_amd"
  else
    DOCKERFILE="Dockerfile"
  fi
  
  if [ "$#" -eq 0 ]; then
    usage;
    exit 1;
  fi
  SKIP_LOCAL_MIRROR=0 
  while getopts "hesxiv:o:" optname; do
    case "$optname" in
      "h")
        usage
        exit 0;
        ;;
      "i")
        SKIP_LOCAL_MIRROR=1
        ;;
      "v")
        VERSION="$OPTARG"
        ;;
      "o")
        DOCKEROPS="$OPTARG"
        ;;
      "?")
        usage;
        exit 1;
        ;;
      *)
      # Should not occur
        echo "Unknown error while processing options inside buildDockerImage.sh"
        ;;
    esac
  done
  echo $VERSION
  cd ${IMAGES_DIR}/patroni 
  dockerfile=${IMAGES_DIR}/patroni/${DOCKERFILE}
  echo "SKIPLOCALMIRROR: $SKIP_LOCAL_MIRROR"
  IMAGE_NAME="patroni-postgres:$VERSION"
  echo "Building image '$IMAGE_NAME' ..."
  build_image $IMAGE_NAME  $dockerfile $SKIP_LOCAL_MIRROR $VERSION
}

main $@